﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Xml;

namespace Useful.GUI
{
    public class RegionMapItem
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool visible = true;
        private Bitmap bmpImage;
        private Bitmap bmpSelected;
        private Bitmap bmpUnSelected;
        private Point location;
        private bool selected = false;
        private string name;
        private Size size;

        public bool Visible
        {
            get
            {
                return visible;
            }
            set
            {
                if (visible != value)
                {
                    visible = value;
                    NotifyPropertyChanged("Visible");
                }
            }
        }

        public bool Selected
        {
            get
            {
                return selected;
            }
            set
            {
                if (visible && selected != value)
                {
                    selected = value;
                    NotifyPropertyChanged("Selected");
                }
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public Size Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        public RegionMapItem(string name, Point location, Bitmap bmpSelected, Bitmap bmpUnSelected)
        {
            Init(name, location, bmpSelected, bmpUnSelected);
        }

        public RegionMapItem()
        {
        }

        public Bitmap Image
        {
            get
            {
                return bmpImage;
            }
        }

        public Point Location
        {
            get
            {
                return location;
            }
        }

        private void Init(string name, Point location, Bitmap bmpSelected, Bitmap bmpUnSelected)
        {
            if (bmpSelected != null && bmpUnSelected != null)
            {
                this.name = name;
                this.location = location;
                this.bmpSelected = bmpSelected;
                this.bmpUnSelected = bmpUnSelected;

                this.Size = bmpImage != null ? bmpImage.Size : Size.Empty;
            }
            else
                throw new ArgumentNullException("bmpSelected == null или bmpUnSelected == null");
        }

        public void Paint(Graphics g)
        {
            if (Visible)
            {
                g.DrawImage(selected ? bmpSelected : bmpUnSelected, location);
            }
        }

        public Color Load(XmlNode node, Color UnSelectedRegionColor, Color SelectedRegionColor)
        {
            bmpImage = (Bitmap)GuiHelper.ResourceManager.GetObject(node["Image"].InnerText, null);

            if (bmpImage != null)
            {
                Init(
                    node["Name"].InnerText,
                    new Point(Convert.ToInt32(node["X"].InnerText), Convert.ToInt32(node["Y"].InnerText)),
                    GDI.ReplaceColor(new Bitmap(bmpImage), SelectedRegionColor),
                    GDI.ReplaceColor(new Bitmap(bmpImage), UnSelectedRegionColor)
                    );

                return Color.FromName(node["Color"].InnerText);
            }
            else
                throw new Exception("RegionMapItem.Load: bmpImage == null");
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}